package com.bdqn.controller;

import com.bdqn.entity.Employee;
import com.bdqn.param.response.Page;
import com.bdqn.service.EmployeeService;
import com.bdqn.util.BaseException;
import com.bdqn.util.Constants;
import com.bdqn.util.RespBean;
import com.bdqn.util.ResultEnum;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * <p>
 * 员工信息 前端控制器
 * </p>
 *
 * @author hunter
 * @since 2022-06-11
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public RespBean login(@RequestBody Employee employee, HttpSession session) {
        System.out.println("获取到的前台传的数据为" + employee.getUsername() + employee.getPassword());
        //进行md5加密 通过spring框架的工具类
        String encryptPassword = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        System.out.println("使用md5加密后的密码为" + encryptPassword);
        /**
         * ①先通过查询用户名是否存在  再继续往下走
         * ②用户名校验成功  密码查询
         * ③查看员工状态是否已禁用
         * ④所有的情况验证成功 存用户信息至session
         */
        //通过用户名查询数据库 这里使用 mybatis   用户是唯一不重复的
        Employee checkUsername = employeeService.checkUsername(employee.getUsername());
        if (checkUsername == null) {
            return RespBean.error("用户名不存在");
        } else {
            if (!checkUsername.getPassword().equals(encryptPassword)) {
                return RespBean.error("密码错误");
            } else {
                if (checkUsername.getStatus() != 1) {
                    return RespBean.error("该员工被禁用");
                } else {
                    System.out.println("最终存入的是" + checkUsername);
                    session.setAttribute(Constants.LOGIN_SESSION, checkUsername.toString());
                    return RespBean.ok("登录成功", checkUsername);
                }
            }
        }
    }

    /**
     * 查询所有的员工信息  并分页
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public RespBean page(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize,String name) {
        Page<Employee> employeePage = employeeService.pageInfo(page, pageSize,name);
        return employeePage != null ? RespBean.ok("获取用户信息列表成功", employeePage) : RespBean.error("获取用户信息列表失败");
    }

    /**
     * 退出用户后台
     */
    @PostMapping("/logout")
    public RespBean logout(HttpSession session) {
        String employee = (String) session.getAttribute(Constants.LOGIN_SESSION);
        if (employee != null) {
            System.out.println("判断是否退出的依据为" + employee);
            //使session无效  或者把session的key删除掉
            session.invalidate();
            return RespBean.ok("退出成功", null);
        } else {
            return RespBean.error("退出失败");
        }
    }

    /**
     * 添加用户信息
     *
     * @param employee
     * @return
     */
    @PostMapping
    public RespBean add(@RequestBody Employee employee, HttpSession session) {
        log.info("获取的添加用户信息为: {}", employee.toString());
        Employee employee1 = employeeService.checkName(employee.getName());
        //通过用户新增的用户名进行查询是否用户已存在
        if (employee1 != null) {
            return new RespBean(ResultEnum.NOT_NAME);
        }
        //进行新增员工信息的密码初始化 使用md5加密后存入数据库
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        Employee user = (Employee) session.getAttribute(Constants.LOGIN_SESSION);
        if(user==null){
            return new RespBean(ResultEnum.AUTH_USER_NOT_LOGIN);
        }
        log.info("获取的创建及修改人的信息为: {}" + user);
        employee.setCreateUser(user.getId());
        employee.setUpdateUser(user.getId());
        if (StringUtils.isEmpty(employee.getPassword())) {
            return new RespBean(ResultEnum.FAIL_PARAM_EMPTY);
        }
        boolean save = employeeService.save(employee);
        return save ? RespBean.ok("添加员工成功", 1) : RespBean.error("添加员工失败");
    }
    //通过id查询对应的对象  进行修改前的回显操作
    @GetMapping("/{id}")
    public RespBean getEmploy(@PathVariable Long id){
        Employee byId = employeeService.getById(id);
        return byId!=null?RespBean.ok("回显前的查询对象成功",byId):RespBean.error("回显前的查询对象失败");
    }
    /**
     * 进行报错修改后的对象
     */
    @PutMapping
    public RespBean update(@RequestBody Employee employee){
        boolean flag = employeeService.saveOrUpdate(employee);
        return flag?RespBean.ok("修改员工信息成功",flag):RespBean.error("修改员工信息失败");
    }
}
